package com.xiaohai.exceptionhandle.exception;

import com.xiaohai.exceptionhandle.common.JsonResponse;
import com.xiaohai.exceptionhandle.common.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice("com.xiaohai.exceptionhandle")
public class ExceptionHandleAdvice {
    Logger LOG = LoggerFactory.getLogger(ExceptionHandleAdvice.class);
    @ExceptionHandler(RuntimeException.class)
    public String runtimeException(RuntimeException e){
        LOG.error("",e);
        return "系统异常";
    }

    @ExceptionHandler(BussnessException.class)
    public String runtimeException(BussnessException e){
        LOG.error("",e);
        return JsonResponse.ERR( e.getCode(),e.getMessage());
    }

    /**
     * 参数绑定异常
     * @param e
     * @return
     */
    @ExceptionHandler(BindException.class)
    public String runtimeException(BindException e){
        LOG.error("",e);
        return JsonResponse.ERR(StatusCode.error,"参数绑定异常");
    }
    /**
     * 参数绑定异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public String runtimeException(Exception e){
        LOG.error("",e);
        return JsonResponse.ERR(StatusCode.error,"位置异常");
    }
}
